package com.toe.recommend.webapi.repository.impl;

import com.toe.pojo.dto.recommend.RecommendClassesRedisDTO;
import com.toe.recommend.webapi.repository.IRecommendClassesRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

@Repository
public class RecommendClassesRepositoryImpl implements IRecommendClassesRepository {

    @Autowired
    private RedisTemplate<String, Serializable> redisTemplate;

    @Override
    public boolean hasClassesDetailsData(Long id) {
        String key = KEY_RECOMMEND_CLASSES_DETAILS + id;
        return redisTemplate.hasKey(key);
    }

    @Override
    public boolean hasClassesDetailsDataList() {
        return redisTemplate.hasKey(KEY_RECOMMEND_CLASSES_DETAILS_LIST);
    }

    @Override
    public void save(RecommendClassesRedisDTO classesVO) {
        String key = KEY_RECOMMEND_CLASSES_DETAILS + classesVO.getId();
        redisTemplate.boundValueOps(key).set(classesVO);
    }

    @Override
    public void saveList(RecommendClassesRedisDTO redisDTO) {
        redisTemplate.opsForList().rightPush(
                KEY_RECOMMEND_CLASSES_DETAILS_LIST, redisDTO);
    }

    @Override
    public void saveList(List<RecommendClassesRedisDTO> listClassesDetails) {
        for (RecommendClassesRedisDTO list : listClassesDetails) {
            redisTemplate.opsForList().leftPush(
                    KEY_RECOMMEND_CLASSES_DETAILS_LIST, list);
        }
    }

    @Override
    public void delete() {
        redisTemplate.delete(KEY_RECOMMEND_CLASSES_DETAILS);
    }

    @Override
    public void delete(Long id) {
        String key = KEY_RECOMMEND_CLASSES_DETAILS + id;
        redisTemplate.delete(key);
    }

    @Override
    public void deleteList() {
        redisTemplate.delete(KEY_RECOMMEND_CLASSES_DETAILS_LIST);
    }

    @Override
    public void deleteList(Long id) {
        redisTemplate.boundListOps(KEY_RECOMMEND_CLASSES_DETAILS_LIST)
                .remove(0, getClassesDetails(id));
    }

    @Override
    public RecommendClassesRedisDTO getClassesDetails(Long id) {
        String key = KEY_RECOMMEND_CLASSES_DETAILS + id;
        return (RecommendClassesRedisDTO) redisTemplate.boundValueOps(key).get();
    }

    @Override
    public List<RecommendClassesRedisDTO> getClassesDetailsList() {
        List<Serializable> range = redisTemplate
                .boundListOps(KEY_RECOMMEND_CLASSES_DETAILS_LIST).range(0, -1);
        if (range == null) {
            return null;
        }
        List<RecommendClassesRedisDTO> list = new ArrayList<>();
        for (Serializable serializable : range) {
            list.add((RecommendClassesRedisDTO) serializable);
        }
        return list;
    }

    @Override
    public int getListCount() {
        return getClassesDetailsList()==null ? 0 : getClassesDetailsList().size();
    }

}
